# -*- coding: utf-8 -*-
import datetime
from django.db import models
from public_util.util.base import BaseModelMixin


class Project(models.Model, BaseModelMixin):
    """项目模型"""
    STATE_TYPE = ((0, "关闭"), (1, "正常"))
    filter_dic = {"project_id": "id__in"}

    pro_name = models.CharField('项目名称', max_length=64, null=False, default="")
    pro_tag = models.CharField('项目代号', max_length=64, null=False, default="", unique=True)
    create_time = models.DateTimeField("创建时间", null=False, auto_now_add=True)
    state = models.SmallIntegerField('状态', null=False, default=1, choices=STATE_TYPE)

    class Meta:
        db_table = u'project'


class Branch(models.Model, BaseModelMixin):
    """分支模型"""
    STATE_TYPE = ((0, "冻结"), (1, "激活"))
    filter_dic = {"project_id": "project__id__in", "state": "state__in"}

    branch_name = models.CharField('分支名称', max_length=64, null=False, default="")
    project = models.ForeignKey(Project, verbose_name='项目', on_delete=models.CASCADE)
    prev_branch = models.ForeignKey('branch', verbose_name='前置分支ID', on_delete=models.CASCADE)
    log_url = models.CharField('日志获取接口', max_length=128, null=False, default="")
    create_time = models.DateTimeField("创建时间", null=False, auto_now_add=True)
    state = models.SmallIntegerField('状态', null=False, default=0, choices=STATE_TYPE)

    class Meta:
        db_table = u'branch'


class Version(models.Model, BaseModelMixin):
    """资源版本模型"""
    OS_TYPE = ((0, "未知"), (1, "Android"), (2, "iOS"), (3, "Windows"), (4, "OSX"))
    MOBILE_OS_TYPE = ((1, "Android"), (2, "iOS"))
    STATE_TYPE = ((0, "关闭"), (1, "正常"))  # 暂时不用
    filter_dic = {"project_id": "branch__project__id__in", "branch_id": "branch__id__in",
                  "plat_type": "os_type__in"}

    ver_name = models.CharField('版本号', max_length=64, null=False, default="")
    branch = models.ForeignKey(Branch, verbose_name='分支名称', on_delete=models.CASCADE)
    res_svn = models.IntegerField('SVN版本号', null=True)
    os_type = models.SmallIntegerField('系统类型', null=False, default=1, choices=OS_TYPE)
    create_time = models.DateTimeField("创建时间", null=False, auto_now_add=True)
    state = models.SmallIntegerField('状态', null=False, default=1, choices=STATE_TYPE)

    @classmethod
    def redo_pack(cls, ver_objs):
        cls(
            ver_name=ver_objs.ver_name,
            branch=ver_objs.branch,
            res_svn=ver_objs.res_svn,
            os_type=ver_objs.os_type,
        ).save()

    class Meta:
        db_table = u'version'


class Task(models.Model, BaseModelMixin):
    """任务模型"""
    TASK_TYPE = (
        (0, "未知"), (1, "开分支"), (2, "打包PC端"), (3, "打包移动端"),
        (4, "发布资源版本"), (5, "冻结分支"), (6, "打包渠道包")
    )
    STATE_TYPE = ((1, "未开始"), (2, "等待"), (3, "进行中"), (4, "中断"), (5, "已完成"), (6, "停止"))
    BREAK_STATE = 4
    STOP_STATE = 6
    PLAT_TYPE_DICT = {"PC": ("Windows", "OSX"), "mobile": ("Android", "iOS")}
    filter_dic = {"project_id": "project__id__in", "branch_id": "branch__id__in",
                  "state": "state__in", "task_type": "task_type__in"}

    task_type = models.SmallIntegerField('任务类型', null=False, default=0, choices=TASK_TYPE)
    project = models.ForeignKey(Project, verbose_name='项目', null=False, on_delete=models.CASCADE)  # 只在开分支的时候用
    branch = models.ForeignKey(Branch, verbose_name='分支', on_delete=models.CASCADE)
    progress = models.SmallIntegerField('进度', null=False, default=0)
    task_json = models.TextField('任务参数', blank=True, default="")
    new_branch = models.CharField('新分支名称', max_length=64, null=True)  # 只在开分支的时候用
    log_url = models.CharField('日志获取接口', max_length=128, null=False, default="")
    create_time = models.DateTimeField("创建时间", null=False, auto_now_add=True)
    state = models.SmallIntegerField('状态', null=False, default=1, choices=STATE_TYPE)

    class Meta:
        db_table = u'task'


class Operate(models.Model, BaseModelMixin):
    """操作类型模型"""
    ope_name = models.CharField('操作类型名称', max_length=64, null=False, default="")
    interface = models.CharField('接口', max_length=180, null=False, default="")
    condition = models.TextField('条件', blank=True, default="")
    info = models.CharField('操作信息', max_length=256, null=False, default="")
    create_time = models.DateTimeField("创建时间", null=False, auto_now_add=True)
    state = models.SmallIntegerField('是否记录', null=False, default=1)

    class Meta:
        db_table = u'operate'


class Handle(models.Model, BaseModelMixin):
    """操作记录模型"""
    user_id = models.IntegerField('用户ID', null=False, default=0)
    ope_id = models.IntegerField('操作ID', null=False, default=0)
    info = models.CharField('操作信息', max_length=256, null=False, default="")
    create_time = models.DateTimeField("创建时间", null=False, db_index=True, auto_now_add=True)

    @classmethod
    def create_handle(cls, user_id, ope_id, **info_dic):
        now = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
        info_dic["create_time"] = now
        ope_obj = Operate.objects.filter(id=ope_id).first()
        cls(
            user_id=user_id,
            ope_id=ope_obj.id,
            info=ope_obj.info.format(**info_dic),
            create_time=now
        ).save()

    class Meta:
        db_table = u'handle'


class Machine(models.Model, BaseModelMixin):
    """机器模型"""
    STATE_TYPE = ((0, "关闭"), (1, "正常"))

    ip = models.CharField("机器IP", null=False, default="", max_length=40)
    cpu_rate = models.SmallIntegerField("CPU使用率", null=False, default=0)
    memory_rate = models.SmallIntegerField("内存使用率", null=False, default=0)
    hd_free = models.CharField("硬盘剩余空间", null=False, default="", max_length=20)
    remark = models.CharField("备注", null=True, default="", max_length=128)
    create_time = models.DateTimeField("创建时间", null=False, db_index=True, auto_now_add=True)
    state = models.SmallIntegerField('状态', null=False, default=1, choices=STATE_TYPE)

    class Meta:
        db_table = u'machine'


class MachineTask(models.Model, BaseModelMixin):
    """机器任务模型"""
    TASK_TYPE = Task.TASK_TYPE
    filter_dic = {"project_id": "project__id__in", "task_type": "task_type__in"}

    machine = models.ForeignKey(Machine, verbose_name='机器', on_delete=models.CASCADE)
    project = models.ForeignKey(Project, verbose_name='项目', on_delete=models.CASCADE)
    task_type = models.SmallIntegerField("任务类型", null=False, default=0, choices=TASK_TYPE)
    create_time = models.DateTimeField("创建时间", null=False, db_index=True, auto_now_add=True)

    class Meta:
        db_table = u'machine_task'


class Channel(models.Model, BaseModelMixin):
    """渠道模型"""
    STATE_TYPE = ((0, "关闭"), (1, "正常"))
    MOBILE_OS_TYPE = ((1, "Android"), (2, "iOS"))
    PROVISION_TYPE = ((0, "ckey"), (1, "dev"), (2, "dis"), (3, "hoc"))
    filter_dic = {"project_id": "project__id__in"}

    project = models.ForeignKey(Project, verbose_name="项目", on_delete=models.CASCADE)
    sdk_name = models.CharField("渠道(SDK)名", null=True, default="", max_length=128)
    plugin_name = models.CharField("工程名", null=True, default="", max_length=128)
    plat = models.SmallIntegerField("系统类型", null=False, default=1, choices=MOBILE_OS_TYPE)
    create_time = models.DateTimeField("创建时间", null=False, db_index=True, auto_now_add=True)
    remark = models.TextField("备注")
    state = models.SmallIntegerField("状态", null=False, default=1, choices=STATE_TYPE)

    class Meta:
        db_table = u'channel'


class Material(models.Model, BaseModelMixin):
    """物料模型"""
    MOBILE_OS_TYPE = ((1, "Android"), (2, "iOS"))
    filter_dic = {"project_id": "project__id__in"}

    project = models.ForeignKey(Project, verbose_name="项目", on_delete=models.CASCADE)
    channel = models.ForeignKey(Channel, verbose_name="渠道", on_delete=models.CASCADE)
    plat = models.SmallIntegerField("系统类型", null=False, default=1, choices=MOBILE_OS_TYPE)
    param_folder = models.CharField("物料名", max_length=128, null=False, default="")
    create_time = models.DateTimeField("创建时间", null=False, db_index=True, auto_now_add=True)

    class Meta:
        db_table = u'material'


class ChannelPackage(models.Model, BaseModelMixin):
    """渠道模型"""
    STATE_TYPE = ((0, "关闭"), (1, "正常"))
    filter_dic = {"project_id": "project__id__in", "branch_id": "branch__id__in", "channel_id": "channel__id__in"}

    project = models.ForeignKey(Project, verbose_name="项目", on_delete=models.CASCADE)
    branch = models.ForeignKey(Branch, verbose_name='分支', on_delete=models.CASCADE)
    channel = models.ForeignKey(Channel, verbose_name="渠道", on_delete=models.CASCADE)
    param_folder = models.CharField("物料名", max_length=128, null=False, default="")
    file_name = models.CharField("文件名", max_length=64, null=False, default="")
    file_link = models.CharField("文件链接", max_length=128, null=False, default="")
    version = models.CharField("版本", max_length=32, null=False, default="")
    create_time = models.DateTimeField("出包时间", null=False, db_index=True, auto_now_add=True)
    state = models.SmallIntegerField("状态", null=False, default=1, choices=STATE_TYPE)

    class Meta:
        db_table = u'channel_package'
